package createmailbox2;

## This file is part of the Simplemail project.
#  Copyright (c) 2008 the Simplemail Team (see authors).
# 
#  For the full copyright and license information, please view the COPYING
#  file that was distributed with this source code. You can also view the
#  COPYING file online at http://unixforum.org.ua/misc/COPYING.txt
# 
#  @copyright  (c) the authors
#  @author     Dmitriy Shilenko <rtyug@ukr.net>
#  @license    GPLv2 http://unixforum.org.ua/misc/COPYING.txt
#  @package    Panel Mailbox
#  @version    $Id: $

use base 'createmailbox', 'func';
use Data::Validate::Domain qw(is_domain);
use Data::Validate::Email qw(is_email);

BEGIN {
    require 'func.pm';
}

use strict;
use CGI::Carp qw(fatalsToBrowser);
use CGI;
use DBI;

use Data::Dumper;

my $func = func->new();

sub new {
    my $self = shift->SUPER::new(@_);
    $self->dbi_connect if $self->can("dbi_connect");
    return $self;
}

sub valid_param_mailbox {
    my $self = shift;

    my %he = ();

    if (   ( !length( $self->{p}->{fDomain} ) )
        || ( 4 > length( $self->{p}->{fDomain} ) )
        || ( 100 < length( $self->{p}->{fDomain} ) )
        || ( !is_domain( $self->{p}->{fDomain} ) ) )
    {
        $he{fDomain_correct} = 1;
    }

    my $dd;
    if ( $he{fDomain_correct} != 1 ) {

        my @sqlu;
        my $cl;
        push @sqlu, $self->{p}->{fDomain}, $self->{p}->{fDomain},
          $self->{p}->{fDomain};
        if ( $self->{session}->{name} ne 'Admin' ) {
            $cl = 'AND idnane = ?';
            push @sqlu, $self->{session}->{user};
        }
        else {

        }

        my $sth = $self->{dbh}->prepare( "
            SELECT t1.domain,
                                  (SELECT count(*) FROM mailbox WHERE domain = ?) AS c_mailbox,
                                  t1.mailboxes,
                                  t1.maxquota,
                                  (SELECT SUM(quota) FROM mailbox WHERE domain = ?) AS mb_all_mailbox,
                                  t2.mb_domain
                         FROM domain AS t1
                         LEFT JOIN admin_domain AS t2
                         ON t1.idnane = t2.idname    
                         WHERE domain = ? 
                         $cl  
                         LIMIT 1
                             " );
        $sth->execute(@sqlu);
        $dd = $sth->fetchrow_hashref();
        $sth->finish();

        if ( !defined( $dd->{domain} ) ) {
            $he{fDomain_correct_exist2} = 1;
        }
    }

    if (   ( !length( $self->{p}->{fUsername} ) )
        || ( 100 < length( $self->{p}->{fUsername} ) )
        || ( $self->{p}->{fUsername} !~ /[A-Za-z0-9]/ )
        || (
            !is_email( $self->{p}->{fUsername} . '@' . $self->{p}->{fDomain} ) )
      )
    {
        $he{fUsername_correct} = 1;
    }

    if (
        ( 200 < length( $self->{p}->{fName} ) )
        || ( ( $self->{p}->{fName} !~ /\w+/ )
            && $self->{p}->{fName} ne '' )
      )
    {
        $he{fName_correct} = 1;
    }

    if (   ( !length( $self->{p}->{fQuota} ) )
        || ( 3 < length( $self->{p}->{fQuota} ) )
        || ( $self->{p}->{fQuota} !~ /^\d+$/ ) )
    {
        $he{fQuota_correct} = 1;
    }

    if ( $he{fQuota_correct} != 1 ) {
        $self->{p}->{fQuota} = $self->{p}->{fQuota} * 1048576;
    }

    if (   ( $self->{session}->{name} ne 'Admin' )
        && ( $he{fDomain_correct} != 1 ) )
    {

        if (   ( $dd->{maxquota} != 0 )
            && ( $self->{p}->{fQuota} == 0 ) )
        {
            $he{MAILBOX_NO_UNLIM} = 1;
        }
    }

    if ( $self->{p}->{fActive} eq 'on' ) {
        $self->{p}->{fActive} = 1;
    }
    else {
        $self->{p}->{fActive} = 0;
    }

    $self->{p}->{maildir} =
        $self->{p}->{fDomain} . '/'
      . $self->{p}->{fUsername} . '@'
      . $self->{p}->{fDomain} . '/';

    $self->{dd} = $dd;
    $self->{he} = \%he;

    return $self;
}

sub valid_param2_mailbox {
    my $self = shift;

    if (   ( !length( $self->{p}->{fPassword} ) )
        || ( 100 < length( $self->{p}->{fPassword} ) )
        || (
            $self->{se}->{max_long_passwd} > length( $self->{p}->{fPassword} ) )
      )
    {
        $self->{he}->{fPassword_correct} = 1;
    }

    if ( $self->{p}->{fPassword} ne $self->{p}->{fPassword2} ) {
        $self->{he}->{fPassword2_correct} = 1;
    }

    if (   ( $self->{session}->{name} ne 'Admin' )
        && ( $self->{he}->{fDomain_correct} != 1 ) )
    {

        if (   ( $self->{dd}->{mailboxes} != 0 )
            && ( $self->{dd}->{c_mailbox} != 0 )
            && ( $self->{dd}->{mailboxes} <= $self->{dd}->{c_mailbox} ) )
        {
            $self->{he}->{MAILBOX_LIMIT} = 1;
        }

        if (   ( $self->{dd}->{maxquota} != 0 )
            && ( $self->{dd}->{maxquota} < $self->{p}->{fQuota} )
            && ( $self->{p}->{fQuota} != 0 ) )
        {
            $self->{he}->{MAILBOX_LIMITMB} = 1;
        }

        if (
               ( $self->{dd}->{mb_domain} > 0 )
            && ( $self->{dd}->{mb_all_mailbox} != 0 )
            && ( $self->{dd}->{mb_all_mailbox} > 0 )
            && ( $self->{dd}->{mb_domain} <=
                $self->{dd}->{mb_all_mailbox} + $self->{p}->{fQuota} )
          )
        {
            $self->{he}->{MAILBOX_ALL_LIMITMB} = 1;
        }
    }

    if ( $self->{he}->{fUsername_correct} != 1 ) {

        my $sth = $self->{dbh}->prepare(
            'SELECT username
                         FROM mailbox 
                         WHERE username = ? 
                         LIMIT 1  
                             '
        );
        $sth->execute( $self->{p}->{fUsername} . '@' . $self->{p}->{fDomain} );
        my $username_sql = $sth->fetchrow_array();
        $sth->finish();

        if ( defined($username_sql) ) {
            $self->{he}->{fusername_correct_exist} = 1;
        }

    }

    return $self;

}

sub insert_mailbox {
    my $self = shift;

    my ( $pwc, $maildir, $salt );
    my @smtp_out = ();

    my $fount;
    foreach my $value ( values( %{ $self->{he} } ) ) {
        if ( $value == 1 ) { $fount = 1; last; }
    }

    if ( !$fount ) {

        if ( $self->{se}->{type_passwd} eq 'md5crypt' ) {
            $self->{p}->{fPassword} =
              $func->md5crypt( $self->{p}->{fPassword} );
        }

        $self->{p}->{email_smtp} =
          $self->{p}->{fUsername} . '@' . $self->{p}->{fDomain};

        my $sth = $self->{dbh}->prepare( '
          INSERT INTO mailbox (username,
                               password,
                               name,
                               maildir,
                               quota,
                               domain,
                               created,
                               modified,
                               active) 
              VALUES (?,?,?,?,?,?,DATETIME("NOW"),DATETIME("NOW"),?)
               ' );

        $sth->execute(
            $self->{p}->{email_smtp}, $self->{p}->{fPassword},
            $self->{p}->{fName},      $self->{p}->{maildir},
            $self->{p}->{fQuota},     $self->{p}->{fDomain},
            $self->{p}->{fActive}
        );
        $sth->finish();

        my $sth = $self->{dbh}->prepare(
            'INSERT INTO alias (address,
                                                  goto,
                                                  domain,
                                                  created,
                                                  modified,
                                                  active) 
                               VALUES (?,?,?,DATETIME("NOW"),DATETIME("NOW"),?)
               '
        );

        $sth->execute(
            $self->{p}->{email_smtp}, $self->{p}->{email_smtp},
            $self->{p}->{fDomain},    $self->{p}->{fActive}
        );
        $sth->finish();

        if ( $self->{p}->{fMail} eq 'on' ) {

            $self->SUPER::sent_email(@_);

        }
        $self->{he}->{mailbox_added} = 1;
        $self->{he}->{EMAIL_ADD}     = $self->{p}->{email_smtp};
    }

    return $self;
}

sub return_create_mailbox {
    shift->SUPER::return_create_mailbox(@_);
}

sub templ_hash_mailbox {
    my $self = shift;

    $self->{session}->{filet} = 'create-mailbox';
    $self->SUPER::templ_hash(@_);
}

sub DESTROY {
    my $self = shift;
    $self->SUPER::DESTROY if $self->can("SUPER::DESTROY");
}

1;
